package com.gz.university.crm.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gz.university.crm.bean.SessionUtils;
import com.gz.university.crm.entity.SysUser;

/**
 * 粗粒度权限控制，检查用户是否登录
 * 
 * @author jack
 * 
 */
public class SessionCheckUserFilter implements Filter {
    private List<String> list;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
        list=new ArrayList<>();
        list.add("/image.jsp");
        list.add("/index.jsp");
        list.add("/popmsg_popedom.jsp");
        list.add("/WEB-INF/page/login.jsp");
        list.add("/sysUserAction!isLogin.action");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// 强制类型转换
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String path=request.getServletPath();
		//System.out.println("path ="+path);
		// 不拦截的路径
		if (list!=null&&list.contains(path)) {
			chain.doFilter(request, response);//放行
			return;
		}
		//获取当前的登录用户
		SysUser sysUser=SessionUtils.getSysUserFromSesion(request);
		if (sysUser!=null) {//不等于空，表示已经登录
			chain.doFilter(request, response);//放行
		}else {
			response.sendRedirect(request.getContextPath()+"/index.jsp");
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
